package com.hs.hlw.credit.system.common.exception;

import com.hualongdata.exception.HlBaseException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hldev on 16-10-12.
 */
public class HlPrivilegeAuthException extends HlBaseException {
    public static final int ERR_CODE = 400002;
    public static final String MESSAGE = "权限校验失败";

    public HlPrivilegeAuthException(BindingResult result) {
        this(ERR_CODE, result, MESSAGE);
    }

    public HlPrivilegeAuthException(int errCode, String errMsg) {
        super(errCode, errMsg);
    }

    public HlPrivilegeAuthException(int errCode, BindingResult result, String errMsg) {
        super(errCode, errMsg);
        List<FieldError> fieldErrors = result.getFieldErrors();
        HashMap<String, String> map = new HashMap<>();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        super.setData(map);
    }
}
